const defaultSettings = require('../../config/defaultSettings')
var app = getApp();
import {getInfo} from "../../api/login"
import {queryTopBannerInfo} from "../../api/banner"
import {queryTopNoticeInfo} from "../../api/notice"
import {queryTopEmpField} from "../../api/field"
import {initDict} from "../../api/dict"
import {queryTopExpertInfo} from '../../api/expert'
import {queryTopEqRepair} from '../../api/eqRepair'
import {queryTopAs} from '../../api/as'
Page({
  data: {
    defaultSettings,
    topBannerList:[],
    topNoticeList:[],
    empFieldList:[],
    topExpertList:[],
    topEqRepairList:[],
    topAsList:[],
    globalData:{},
    worker:null,
    globalData:{},


  },
  /**
   * 页面显示时触发，页面显示/切入前台时触发
   */
  onShow() {
    let token = wx.getStorageSync(defaultSettings.tokenKey)
    if(token && app.globalData.appInfo.emp){
      //设备报修信息
      // this.onQueryTopEqRepair()
    }
  },
  /**
   * 页面初次渲染完成时触发(一个页面只会调用一次)
    代表页面已经准备妥当，可以和视图层进行交互
   */
  onReady(){
    
  },
  /**
   * 页面加载时触发 (一个页面只会调用一次)
   */
  onLoad() {
    wx.setNavigationBarTitle({
      title: "希尔泰智慧灌溉系统",
    })
    //轮播图
    this.onQueryTopBannerInfo()
    //地理信息
    this.onCreateLocation()

    let token = wx.getStorageSync(defaultSettings.tokenKey);
    if(token){
      //如果token 存在 查询用户信息
      getInfo().then(res=>{
        if(res.success){
          app.globalData.appInfo = res.result
          wx.setStorage({
            key:defaultSettings.tokenKey,
            data:res.result.token
          })
        }
      })
    }
  },
  onQueryTopBannerInfo(){
    queryTopBannerInfo().then(res=>{
      if(res.success){
        this.setData({topBannerList:res.result.records})
      }
    })
  },
  // onQueryTopExpertInfo(){
  //   queryTopExpertInfo().then(res=>{
  //     if(res.success){
  //       this.setData({topExpertList:res.result.records})
  //     }
  //   })
  // },
  // onQueryTopNoticeInfo(){
  //   queryTopNoticeInfo().then(res=>{
  //     if(res.success){
  //       this.setData({topNoticeList:res.result.records})
  //     }
  //   })
  // },

  // onQueryTopEmpField(){
  //   queryTopEmpField().then(res=>{
  //     if(res.success){
  //       this.setData({empFieldList:res.result.records})
  //     }
  //   })
  // },

  // onQueryTopEqRepair(){
  //   queryTopEqRepair().then(res=>{
  //     if(res.success){
  //       this.setData({topEqRepairList:res.result.records})
  //     }
  //   })
  // },


  // onOpenNoticeDetailPage(e){
  //   wx.setStorageSync("noticeItem", e.mark.item);
  //   wx.navigateTo({
  //     url: '/subpackages/parsepackage/pages/notice/noticeDetail',
  //   })
  // },

  // onQueryTopAs(){
  //   queryTopAs().then(res=>{
  //     if(res.success){
  //       this.setData({topAsList:res.result.records})
  //     }
  //   })
  // },

  onOpenNoticeListPage(){
    wx.navigateTo({
      url: '/pages/notice/noticeList',
    })
  },

  onOpenEqListPage(){
    let token = wx.getStorageSync(defaultSettings.tokenKey);
    if(!token){
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/eq/eqItem',
      })
    }
    
  },

  onOpenEqReapirPage(){
    let token = wx.getStorageSync(defaultSettings.tokenKey);
    if(!token){
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/eqRepair/eqRepairDetial',
      })
    }

    
  },
  onOpenAddEqPage(){
    let token = wx.getStorageSync(defaultSettings.tokenKey);
    if(!token){
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/baoxiu/addEq',
      })
    }
    
  },

  onBannerDetailPage(e){
    wx.setStorageSync("bannerItem", e.mark.item);
    wx.navigateTo({
      url: '/subpackages/parsepackage/pages/banner/bannerDetail',
    })
  },

  // onOpenExpertDetailPage(e){
  //   wx.setStorageSync("expertItem", e.mark.item);
  //   wx.navigateTo({
  //     url: '/pages/expert/expertDetail',
  //   })
  // },

  onOpenExpertListPage(){
    wx.navigateTo({
      url: '/pages/expert/expertList',
    })
  },
  onOpenRepairListPage(){
    wx.navigateTo({
      url: '/pages/eqRepair/eqRepairList',
    })
  },
  onOpenRepairDetailPage(e){
    wx.setStorageSync("eqRepairItem", e.mark.item);
    wx.navigateTo({
      url: '/pages/eqRepair/eqRepairDetial',
    })
  },
  onOpenAccountPage(){
    wx.showToast({icon:"none",title: "暂未开放"})
    return false
  },
  onOpenIrrigationDataPage(){
    wx.showToast({icon:"none",title: "暂未开放"})
    return false
  },
  onOpenShopPage(){
    wx.showToast({icon:"none",title: "暂未开放"})
    return false
  },
  onNotOpen(){
    wx.showToast({icon:"none",title: "暂未开放"})
    return false
  },
  //构造地理信息下拉
  onCreateLocation(){
    let that = this
    initDict(this.data.defaultSettings.dictCodes.locationTree).then(res=>{
        that.setData({globalData:getApp().globalData});
      }
    )
  },

  onOpenFieldListPage(){
    wx.navigateTo({
      url: '/pages/field/fieldList',
    })
  },
  
  onOpenPumpWellListPage(e){
    wx.navigateTo({
      url: '/pages/fieldeq/fieldEqList?fieldNo='+e.mark.item.fieldNo,
    })
  },


  initWebsocket(){
    let that = this
    let token = wx.getStorageSync(defaultSettings.tokenKey)
    let emp = getApp().globalData.appInfo.emp
    if(!token || !emp || !defaultSettings.websocket){
      return false
    }
    let empNo = emp.empNo
    that.data.websocket = wx.connectSocket({
      url: defaultSettings.websocket,
      header:{
        'content-type': 'application/json'
      },
      success(res) {
        console.log('链接成功', res);
      },
      fail(res) {
        console.log('链接失败', res);
      },
    })

    that.data.websocket.onOpen((r)=>{
      that.data.webSocketIsOpen = true
      let request = {}
      request.empNo = empNo
      that.data.websocket.send({
        data:JSON.stringify(request),
        success(res) {
          console.log('发送成功', res);
        },
        fail(res) {
          console.log('发送失败', res);
        },
      })
    })
    that.data.websocket.onError((r)=>{
      that.data.webSocketIsOpen = false
      that.websocketReconnect()
    })

    //接受服务端数据
    that.data.websocket.onMessage((res)=>{
      let data = null
      if(res.data){
        data = res.data
      }
      if(!data){
        return false
      }
      data = JSON.parse(data)
      getApp().globalData.websocketData[data.type] = data.data
      this.setData({websocketData:getApp().globalData.websocketData})
    })
    that.data.websocket.onClose(() => {
      that.data.webSocketIsOpen = false
      that.websocketReconnect()
    })
  },
  //websocket重连
  websocketReconnect(){
    let that = this
    if (that.data.heartbeatTimeout) {
      clearTimeout(that.data.heartbeatTimeout)
    }
    if(!that.webSocketIsOpen) {
      that.data.heartbeatTimeout = setTimeout(() => {
        console.log("正在重连，当前时间" + new Date())
        that.initWebsocket()
      }, 5000)
    }
  },
})
